package com.review.test;

import java.util.Arrays;
import java.util.HashSet;

public class MyTest03 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param edges int整型一维数组 边长
     * @return int整型
     */
    public int NumOfTriangle(int[] edges) {
        // write code here
        if (edges == null || edges.length < 3) {
            return 0;
        }

        int count = 0;
        Arrays.sort(edges);
        int n = edges.length;
        for (int i = n - 1; i >= 2; i--) {
            int l = 0, r = i - 1;
            while (l < r) {
                if (edges[l] + edges[r] > edges[i]) {
                    count += r - l;
                    --r;
                } else {
                    ++l;
                }
            }
        }
        return count;
    }

    public int NumOfTriangle2(int[] edges) {
        // write code here
        if (edges == null || edges.length < 3) {
            return 0;
        }

        Arrays.sort(edges);
        HashSet<String> set = new HashSet<>();
        for (int i = 0; i < edges.length - 2; i++) {
            for (int j = i + 1; j < edges.length; j++) {
                for (int k = j + 1; k < edges.length; k++) {
                    if (edges[i] + edges[j] > edges[k]&&edges[i] + edges[k] > edges[j]&&edges[j] + edges[k] > edges[i]){
                        //去重
                        set.add(edges[i]+""+edges[j]+""+edges[k]);
                    }
                }
            }
        }
        return set.size();
    }
}
